package com.yuzhi.master.portableDevice.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;
import com.yuzhi.master.portableDevice.domain.PortableDevice;
import com.yuzhi.master.portableDevice.domain.bo.PortableDeviceBo;
import com.yuzhi.master.portableDevice.domain.request.PortableDeviceReq;
import com.yuzhi.master.portableDevice.domain.vo.PortableDeviceVo;
import com.yuzhi.master.portableDevice.mapper.PortableDeviceMapper;
import com.yuzhi.master.portableDevice.service.IPortableDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 便携式设备信息Service业务层处理
 *
 * @author auto code generator
 * @date 2025-08-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PortableDeviceServiceImpl implements IPortableDeviceService {

    @Autowired
    private PortableDeviceMapper baseMapper;

    /**
     * 查询便携式设备信息
     */
    @Override
    public PortableDeviceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询便携式设备信息列表
     */
    @Override
    public PageDataInfo<PortableDeviceVo> queryPageList(PortableDeviceReq request) {
        LambdaQueryWrapper<PortableDevice> lqw = buildQueryWrapper(request);
        Page<PortableDeviceVo> result = baseMapper.selectVoPage(request.getPageQuery().build(), lqw);
        return PageDataInfo.build(result);
    }

    /**
     * 查询便携式设备信息列表
     */
    @Override
    public List<PortableDeviceVo> queryList(PortableDeviceReq request) {
        LambdaQueryWrapper<PortableDevice> lqw = buildQueryWrapper(request);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PortableDevice> buildQueryWrapper(PortableDeviceReq request) {
        Map<String, Object> params = request.getParams();
        LambdaQueryWrapper<PortableDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getName()), PortableDevice::getName, request.getName());
        lqw.eq (StringUtils.isNotBlank(request.getDeviceId()), PortableDevice::getDeviceId, request.getDeviceId());
        lqw.between(params.get("beginPurchaseTime") != null && params.get("endPurchaseTime") != null,
            PortableDevice::getPurchaseTime ,params.get("beginPurchaseTime"), params.get("endPurchaseTime"));
        return lqw;
    }

    /**
     * 新增便携式设备信息
     */
    @Override
    public Boolean insertByBo(PortableDeviceBo bo) {
        PortableDevice add = BeanUtil.toBean(bo, PortableDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setManufacturer(add.getManufacturer());
        }
        return flag;
    }

    /**
     * 修改便携式设备信息
     */
    @Override
    public Boolean updateByBo(PortableDeviceBo bo) {
        PortableDevice update = BeanUtil.toBean(bo, PortableDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PortableDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除便携式设备信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据id删除便携式设备信息信息
     */
    @Override
    public Boolean deleteById(String id, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteById(id) > 0;
    }
}
